home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / arrays.arc / ARRAYS.PRN next >
Encoding:
Text File  |  1987-09-17  |  12.2 KB  |  481 lines

  1.  
  2.  
  3.  
  4.                                ARRAYS IN BASIC                                
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.                               TABLE OF CONTENTS
  17.                               =================
  18.  
  19.  
  20.  
  21.  
  22.                       1. Arrays as Variables            p.1
  23.  
  24.                       2. Examples of Array Usage        p.6
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.      ----------------------------------------------------------------
  55.          This document was written by Ken Karp for posting on the
  56.     QuickBASIC conference of MicroSellar BBS.  Thanks to Terri Karp for
  57.     valuable assistance in editing.  For more information about BASIC
  58.     programming, and QuickBASIC in particular, register with the
  59.     MicroSellar BBS at (201) 239-1346 (modem).  Registration is free.
  60.     Participation is welcome.
  61.  
  62.  
  63.  
  64.                                ARRAYS IN BASIC                                
  65.  
  66.  
  67.  
  68.     
  69.  
  70.     1. ARRAYS AS VARIABLES
  71.     ----------------------
  72.  
  73.          When a BASIC program is running, the first time a reference is
  74.     made to a particular variable, a `box' is created for that variable in
  75.     the computer's memory.
  76.  
  77.     Examine the following BASIC program segment:
  78.  
  79.                                               '  / Input a value from the
  80.         INPUT VAR1%                           ' <  keyboard and set VAR1
  81.                                               '  \ equal to it
  82.         VAR1% = VAR1% + 10                    'manipulate the variable
  83.         PRINT "The value of VAR1 is";VAR1%    'output the value
  84.  
  85.  
  86.     Let's focus in on the first statement.  If this is the first
  87.     reference to VAR1%, the computer will set aside a box for it:
  88.  
  89.                            ┌──────┐
  90.                     VAR1%  │      │
  91.                            └──────┘
  92.  
  93.     When we think of the value of VAR1%, we can think of whatever is inside
  94.     the box.  Thus, when the BASIC statement
  95.  
  96.                                               '  / Input a value from the
  97.         INPUT VAR1%                           ' <  keyboard and set VAR1
  98.                                               '  \ equal to it
  99.  
  100.     is encountered, a check is made to see if a box had previously been
  101.     created for VAR1%.  If no box is found, one will be created, and only
  102.     after that is done will the "INPUT" part of the statement be performed.
  103.     If the user enters the value 21 to the INPUT prompt, we will have the
  104.     following:
  105.                            ┌──────┐
  106.                     VAR1%  │  21  │
  107.                            └──────┘
  108.  
  109.          In light of all this, our second statement takes on the following
  110.     meaning:  "go get the value that is stored in the box for VAR1%;  add
  111.     10 to it;  store the result back in the box for VAR1%".  Thus, after
  112.     the statement
  113.  
  114.         VAR1% = VAR1% + 10                    'manipulate the variable
  115.  
  116.  
  117.  
  118.                                       1                                       
  119.  
  120.  
  121.  
  122.  
  123.  
  124.                                ARRAYS IN BASIC                                
  125.  
  126.  
  127.  
  128.     has executed, we have:
  129.                            ┌──────┐
  130.                     VAR1%  │  31  │
  131.                            └──────┘
  132.  
  133.  
  134.         Finally, the third statement
  135.  
  136.         PRINT "The value of VAR1 is";VAR1%    'output the value
  137.  
  138.     will get the value that is stored in the box for VAR1%; and then print
  139.     it.  Although VAR1% box is referenced by this statement, its contents
  140.     remain unchanged, and as our program segment finishes, we still have:
  141.                            ┌──────┐
  142.                     VAR1%  │  31  │
  143.                            └──────┘
  144.  
  145.  
  146.          Pursuing this analogy, an array is nothing more than a whole bunch
  147.     of the same type of boxes.  The BASIC DIM statement is used to announce
  148.     that a certain number of boxes are being set aside, AND that they are
  149.     to be referred to by a certain name.  Thus, the BASIC statement
  150.  
  151.             DIM GRADES%(10)
  152.  
  153.     will set aside 10 [*] integer boxes each of which are to be referred
  154.     to using the name GRADES%.  Each of the 10 boxes is capable of holding
  155.     a value, just like VAR1% in the example above.  In effect, we can
  156.     imagine the boxes to look something like this:
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.      ----------------------------------------------------------------
  173.     * -  Actually, the statement will set aside 11 boxes, numbered 0
  174.     through 10; but for purposes of this discussion, it is simpler to
  175.     assume 10 boxes.  QuickBASIC users, look up OPTION BASE in your manual.
  176.  
  177.  
  178.                                       2                                       
  179.  
  180.  
  181.  
  182.  
  183.  
  184.                                ARRAYS IN BASIC                                
  185.  
  186.  
  187.  
  188.                            GRADES%
  189.  
  190.                           ┌──────┐
  191.                        1  │      │
  192.                           ├──────┤
  193.                        2  │      │
  194.                           ├──────┤
  195.                        3  │      │
  196.                           ├──────┤
  197.                        4  │      │
  198.                           ├──────┤
  199.                        5  │      │
  200.                           ├──────┤
  201.                        6  │      │
  202.                           ├──────┤
  203.                        7  │      │
  204.                           ├──────┤
  205.                        8  │      │
  206.                           ├──────┤
  207.                        9  │      │
  208.                           ├──────┤
  209.                       10  │      │
  210.                           └──────┘
  211.  
  212.  
  213.          But how (the attentive student will ask) are the 10 boxes
  214.     distinguished from each other if they are all called GRADES%?  Simple:
  215.     by putting the number of the box desired in parentheses immediately
  216.     following the word GRADES%.  Thus,
  217.  
  218.             FIRSTGRADE% = GRADES%(1)
  219.  
  220.     will find the first of the 10 boxes associated with the name GRADES%,
  221.     extract the value currently found there, and place it into some other
  222.     box, called FIRSTGRADE%.
  223.  
  224.             GRADES%(4) = FOURTHONE%
  225.  
  226.     will copy the value currently in the box named FOURTHONE% into the
  227.     fourth box associated with the variable name GRADES%.
  228.  
  229.          In BASIC, the number inside the parentheses is called the
  230.     "subscript", and arrays are often called "subscripted variables".  It
  231.     is important to note that the subscript itself may be a variable.
  232.     Observe the following program segment:
  233.  
  234.  
  235.  
  236.  
  237.  
  238.                                       3                                       
  239.  
  240.  
  241.  
  242.  
  243.  
  244.                                ARRAYS IN BASIC                                
  245.  
  246.  
  247.  
  248.             PRINT "Here are the class's grades:"    'print an introduction
  249.             FOR N%=1 TO 10                         'do something 10 times
  250.                 PRINT GRADES%(N%)            'print what's in the Nth box
  251.             NEXT
  252.  
  253.     If we had:
  254.                            GRADES%
  255.  
  256.                           ┌──────┐
  257.                        1  │  94  │
  258.                           ├──────┤
  259.                        2  │  97  │
  260.                           ├──────┤
  261.                        3  │  83  │
  262.                           ├──────┤
  263.                        4  │  91  │
  264.                           ├──────┤
  265.                        5  │  79  │
  266.                           ├──────┤
  267.                        6  │  82  │
  268.                           ├──────┤
  269.                        7  │  92  │
  270.                           ├──────┤
  271.                        8  │  88  │
  272.                           ├──────┤
  273.                        9  │ 100  │
  274.                           ├──────┤
  275.                       10  │  89  │
  276.                           └──────┘
  277.  
  278.     prior to executing the loop, we would see results something like this
  279.     on our screen:
  280.  
  281.             Here are the class's grades:
  282.              94
  283.              97
  284.              83
  285.              91
  286.              79
  287.              82
  288.              92
  289.              88
  290.              100
  291.              89
  292.  
  293.     Why?  Since the variable N% is used as the counter in the FOR loop,
  294.     each time the statement:
  295.  
  296.  
  297.  
  298.                                       4                                       
  299.  
  300.  
  301.  
  302.  
  303.  
  304.                                ARRAYS IN BASIC                                
  305.  
  306.  
  307.  
  308.                 PRINT GRADES%(N%)            'print what's in the Ith box
  309.  
  310.     is executed, N% has a value one greater than the last time it was
  311.     executed;  therefore, the SUBSCRIPT into GRADES% causes the program to
  312.     look at the NEXT numbered box.
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.                                       5                                       
  359.  
  360.  
  361.  
  362.  
  363.  
  364.                                ARRAYS IN BASIC                                
  365.  
  366.  
  367.  
  368.     
  369.  
  370.     2. EXAMPLES OF ARRAY USAGE
  371.     --------------------------
  372.  
  373.          There are three QuickBASIC examples of array usage distributed
  374.     with this discussion.  They are
  375.  
  376.                         ONE-D.BAS
  377.                         TWO-D.BAS
  378.                         THREE-D.BAS
  379.  
  380.     ONE-D.BAS employs a one dimensional array called CITIES$().  A one
  381.     dimensional array in BASIC uses only one subscript in all references to
  382.     the array (in the above example GRADES%() is a one dimensional array).
  383.     ONE-D.BAS reads in a list of city names from DATA statements and stores
  384.     them in CITIES$().  It then prints out the same list of cities from
  385.     CITIES$().
  386.  
  387.          If a BASIC array has two subscripts it is referred to as a two
  388.     dimensional array.  An example of a two dimensional array is TEMPS(),
  389.     found in TWO-D.BAS.  The statement
  390.  
  391.                 DIM  ...., TEMPS(100,4)
  392.  
  393.     sets aside storage for a 100x4 (actually 101x5 -- TEMPS(0,0) is valid
  394.     and may be used if desired!!) array.  Notice the FOR-NEXT loop in the
  395.     subroutine READINDATA:  each time the loop counter (I) varies (ie, with
  396.     each iteration of the loop), we read in an entire row of data (ie,
  397.     TEMPS(I,1), TEMPS(I,2), TEMPS(I,3), & TEMPS(I,4)).  The same stragegy
  398.     is used when the values are printed out.
  399.  
  400.          The final example program, THREE-D.BAS, takes us one step further:
  401.     now we have added a third dimension.  TEMPS() uses its first dimension
  402.     for the city; its second dimension for the particular data in question
  403.     (ie, high temperature, low temperature, rainfall, or wind speed); and
  404.     its third dimension for the day of the week (Sunday through Saturday).
  405.     Just as a two dimensional array may be conceptualized as a table of
  406.     values, a three dimensional array may be conceptualized as an
  407.     accountant's ledger: when the ledger is open to Sunday, you see before
  408.     you a table of figures (a two dimensional array!);  when you turn the
  409.     page to Monday, you see a similar table, but the actual figures may
  410.     differ;  when you turn to Tuesday, again the figures may differ.  In
  411.     actual fact, TEMPS() is a 4x100x6 structure: 2400 integers, all
  412.     referenced by the name TEMPS().
  413.  
  414.          The program THREE-D.BAS prints all the weather information for the
  415.     22 cities for a certain day of the week.  By pressing <PgUp> or <PgDn>
  416.  
  417.  
  418.                                       6                                       
  419.  
  420.  
  421.  
  422.  
  423.  
  424.                                ARRAYS IN BASIC                                
  425.  
  426.  
  427.  
  428.     the user can print the same information for a different day of the
  429.     week.  To do this, the subroutine PRINTOUTDATA holds the third
  430.     subscript steady (variable DAY is set before PRINTOUTDATA is called)
  431.     while varying the other two subscripts.  This yields a two dimensional
  432.     cross-section of our three dimensional array: ie, all the weather
  433.     information for all the cities, but only for a particular day.  In
  434.     fact, that is the two dimensional array we see on the screen.
  435.  
  436.  
  437.          QuickBASIC users will note that v3.0 allows up to 63 dimensions
  438.     for an array!  That is to say
  439.  
  440.                 DIM ARRAYNAME (d1,d2,d3,d4, ... ,d63)
  441.  
  442.     would set aside storage for d1xd2xd3xd4x ... xd63 array elements -- a
  443.     very large data structure indeed!  In actual practice, arrays of more
  444.     than two or three dimensions are rarely used.  It should be easy to see
  445.     why.
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.                                       7                                       
  479.  
  480.  
  481.